package com.rc.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Category;
import com.rc.activity.provider.Contract;
import com.rc.adapter.CategorizeItemAdapter;
import com.rc.custom.MyListView;
import com.rc.db.DatabaseSupport;
import com.telpoo.frame.ui.BaseActivity;

public class CategorizeItems extends BaseActivity implements
		OnItemClickListener, TextWatcher {

	public static void callIntent(Activity ct) {
		Intent it = new Intent(ct, CategorizeItems.class);
		ct.startActivityForResult(it, 1);
	}

	// call by trip list
	public static void callIntentCategorize(Activity ct,
			ArrayList<String> arrItems, long mTripID) {
		Intent it = new Intent(ct, CategorizeItems.class);
		it.putExtra("TRIP_ID", mTripID);
		Bundle value = new Bundle();
		value.putStringArrayList("ITEM_ID", arrItems);
		it.putExtras(value);
		ct.startActivityForResult(it, 1);
	}

	private ArrayList<tbl_Category> arrCategory = null;
	private MyListView lvCategorize;
	private TextView txtSearch;
	private CategorizeItemAdapter adpCategorize;
	private long mTripID = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.categorize_list);

		lvCategorize = (MyListView) findViewById(R.id.lvCategorize);
		txtSearch = (TextView) findViewById(R.id.txtSearch);

		arrCategory = new ArrayList<tbl_Category>();
		Cursor cursor = this.getContentResolver().query(
				Contract.CONTENT_URI_CATEGORY, null, null, null, null);
		arrCategory = tbl_Category.getObjectbyCursor(cursor);
		adpCategorize = new CategorizeItemAdapter(this, arrCategory);
		lvCategorize.setAdapter(adpCategorize);
		lvCategorize.setOnItemClickListener(this);
		txtSearch.addTextChangedListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (getIntent().hasExtra("TRIP_ID")) {
			mTripID = getIntent().getLongExtra("TRIP_ID", 0);
			ArrayList<String> arrItems = getIntent().getStringArrayListExtra(
					"ITEM_ID");
			if (DatabaseSupport.updateItemsCategoryID(arrItems, arrCategory.get(position)
					.getCategoryID(), CategorizeItems.this, mTripID) > 0) {
				Intent i = getIntent();
				setResult(1, i);
				finish();
			}
		} else {
			Intent i = getIntent();
			i.putExtra("CATEGORY_ID", arrCategory.get(position).getCategoryID());
			setResult(1, i);
			finish();
		}

	}

	public ArrayList<tbl_Category> SearchListView(
			ArrayList<tbl_Category> arrCategoryObj, String contentSearch) {
		ArrayList<tbl_Category> mArrCategoryList = new ArrayList<tbl_Category>();
		for (int i = 0; i < arrCategoryObj.size(); i++) {
			if (arrCategoryObj.get(i).getName().toUpperCase()
					.contains(contentSearch.toUpperCase())) {
				mArrCategoryList.add(arrCategoryObj.get(i));
			}

		}
		return mArrCategoryList;
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (txtSearch.getText().toString().length() > 0) {
			adpCategorize.NotiDB(SearchListView(arrCategory, txtSearch
					.getText().toString()));
		} else {
			adpCategorize.NotiDB(arrCategory);
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

}
